package com.ihopper.service.impl;

import com.ihopper.pojo.User;
import com.ihopper.mapper.UserMapper;
import com.ihopper.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ihopper.utils.CookieUtil;
import com.ihopper.utils.MD5Util;
import com.ihopper.utils.UUIDUtil;
import com.ihopper.utils.ValidatorUtil;
import com.ihopper.vo.LoginVo;
import com.ihopper.vo.RespBean;
import com.ihopper.vo.RespBeanEnum;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ihopper
 * @since 2021-07-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String username=loginVo.getMobile();
        String password=loginVo.getPassword();
        //根据手机号获取用户
        User user = userMapper.selectById(username);
        if(user==null){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);

        }
        //判断密码是否正确
        if(!MD5Util.fromPassToDBPass(password,user.getSlat()).equals(user.getPassword())){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        //生成cookie
        String ticket = UUIDUtil.uuid();
        request.getSession().setAttribute(ticket,user);
        CookieUtil.setCookie(request,response,"userTicket",ticket);
        return RespBean.success();

    }
}
